// SliceMarker.cpp : Defines the entry point for the console application.
//


#include "stdafx.h"
#include "ROI.h"


using namespace log4cplus;
#pragma comment(lib, "mpir.lib")
bool CheckRegcode(CString sRegCode)
{
	 Logger logger = Logger::getInstance("test.subtestof_filelog"); 
	 T_DEVICE_PROPERTY HDD_Description;
	 T_DEVICE_PROPERTY CPU_Description;
     BOOL bok = WMI_DeviceQuery(1,&HDD_Description,1);
	 BOOL bok2 = WMI_DeviceQuery(3,&CPU_Description,1);
	 USES_CONVERSION;
	 if( !bok)
		 return false;
	 if( !bok2)
		 return false;

	 string sCPU = T2A(CPU_Description.szProperty);
     string sHDD = T2A(HDD_Description.szProperty);
	 string sPC = "";
	 for( int i = 0; i< sHDD.length(); i++)
	 {
		 sPC += sHDD[i];
		 if( sCPU.length() > i)
			 sPC += sCPU[i];
	 }
	 
	 string sNewPC = "";
	 std::transform(sPC.begin(), sPC.end(),sPC.begin(), ::toupper);
	 for (int i = 0; i < sPC.length(); i += 3)
     {
		 char buf[2];
		 sprintf(buf,"%d",sPC[i]);
		 sNewPC += buf;
	 }
	  
	 mpz_t bigVal;
	 mpz_t nextVal;
	 mpz_t regBarcode;
	 mpz_init(bigVal);
	 mpz_init(nextVal);
	 mpz_init(regBarcode);
	 mpz_set_str(bigVal,sNewPC.c_str(),10);
	 mpz_set_str(regBarcode,sRegCode.GetBuffer(255),10);
	 sRegCode.ReleaseBuffer();

	 mpz_nextprime (nextVal, bigVal);
	 return regBarcode == nextVal;
}

int _tmain(int argc, _TCHAR* argv[])
{
	 
	 
	 ROI roi;
	 CString sFolder = roi.GetExeFolder();
	 sFolder += "\\logs\\log.txt";
	 tstring slogFile(sFolder.GetBuffer(255));
	 sFolder.ReleaseBuffer();
	 SharedAppenderPtr fileAppender(new DailyRollingFileAppender(slogFile, MINUTELY, true, 5));    
  
	 fileAppender->setName("file log test");   
	 Logger logger = Logger::getInstance("test.subtestof_filelog"); 
	 auto_ptr<Layout> pPatternLayout(new PatternLayout(_T("%d{%m/%d/%y %H:%M:%S}    - %m [%l]%n"))); 
	 fileAppender->setLayout(pPatternLayout);
	 logger.addAppender(fileAppender);
	 LOG4CPLUS_INFO( logger,"============= starts ===================");
	 

	if( argc != 4)
	{
		LOG4CPLUS_INFO( logger,"argument number is not 4, returns -1.\r\n";);
		return -1;
	}
	
	CString sArg2 = argv[1];
	bool bOk = CheckRegcode(sArg2);
	if( !bOk)
	{
		LOG4CPLUS_INFO( logger,"password incorrect, returns -2.\r\n");
		return -2;
	}
	int nSample = atoi(argv[3]);
	if( nSample < 1 || nSample > 10)
	{
		LOG4CPLUS_INFO( logger,"sample number not between 1 & 10, return -3.\r\n");
		return -3;
	}
	
	IplImage* src = cvLoadImage( argv[2] );
	vector<IplImage*>subImages = roi.ExtractROIs(src,nSample);
	vector<pair<int,int>> marks;
	for( int i = 0; i< subImages.size(); i++)
	{
		CString sSub = "";
		sSub.Format("%s%d",subImgFileName,i+1);
		SobelProcessor processor(subImages[i],sSub);
		marks.push_back(processor.MarkPeaks());
		
		cvReleaseImage(&subImages[i]);
	}
	CString sExeFolder = roi.GetExeFolder();
	CString sResFile = sExeFolder + "\\Temp\\result.txt";
	ofstream ofs(sResFile);
	for( int i = 0; i< marks.size(); i++)
	{
		ofs<<marks[i].first<<" "<<marks[i].second<<endl;
	}
	cvReleaseImage(&src);
	return 0;
}

